package com.zhiyou100.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.entity.Contract;
import com.zhiyou100.service.ContractService;
import com.zhiyou100.util.object.ObjectUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 合同 action
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-08-29 15:50
 */
@Controller
@RequestMapping("/contract")
public class ContractAction {
    private ContractService contractService;

    @Autowired
    public void setContractService(ContractService contractService) {
        this.contractService = contractService;
    }

    @RequestMapping("/list.action")
    public String listHouses(Model model,
                             @RequestParam(defaultValue = "1", value = "pageNo") Integer pageNo
    ) {
        // 每页显示的记录数
        int pageSize = 10;

        // 分页查询
        PageHelper.startPage(pageNo, pageSize);

        // 获取所有的信息
        final List<Contract> list = contractService.listContract();

        // 使用 PageInfo<Contract> 结果进行封装
        final PageInfo<Contract> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfoContract", pageInfo);
        return "view/contract/list";
    }

    @RequestMapping("/getOneByCid.action")
    public String getOneByHid(Integer cid, String method, Model model) {
        final Contract contract = contractService.selectOneByCid(cid);
        if (contract != null) {
            // 有，放入区域中，跳转 到 详情页面
            if (method == null || method.trim().length() == 0) {
                // 跳转到 view/contract/detail
                model.addAttribute("contract", contract);
                return "view/contract/detail";
            } else {
                // 跳转到更新界面
                model.addAttribute("contract", contract);
                return "view/contract/update";
            }

        } else {
            // 没有，返回列表
            return "view/contract/list";
        }
    }

    /**
     * 插入一条记录
     *
     * @param contract 实体类
     * @param request  域属性
     * @return /view/house/list
     */
    @SneakyThrows
    @RequestMapping("/insertOne.action")
    public String insertOne(Contract contract, HttpServletRequest request) {
        // Contract(cid=null, lid=1, hid=1, ccash=1, ccreatetime=Wed Aug 05 18:35:00 CST 2020, cstarttime=Sun Aug 02 17:35:00 CST 2020, cendtime=Sun Aug 02 17:35:00 CST 2020, ctotalmoney=1000.0, cstatus=1, ctype=1)

        String id = "cid";
        if (ObjectUtil.onlyOneFiledIsNullAndTheFiledIsId(contract, id)) {
            // 有且只有 cid 一个字段为空，合法
            contractService.insertOne(contract);
        }

        return "/view/contract/list";
    }

    @RequestMapping("/updateOneByCid.action")
    public String updateOneByCid(Contract contract) {
        // Contract(cid=null, lid=1, hid=1, ccash=1, ccreatetime=Wed Apr 01 13:00:00 CST 2020, cstarttime=Fri May 01 13:00:00 CST 2020, cendtime=Tue Sep 01 13:00:00 CST 2020, ctotalmoney=1.0, cstatus=1, ctype=1)
        if (ObjectUtil.getNullFiled(contract).isEmpty()) {
            // 没有字段为空，更新成功
            contractService.updateOneContract(contract);
        }


        return "/view/contract/list";
    }

    /**
     * 自定义一个 日期属性编辑器
     * yyyy-MM-dd'T'HH:mm:ss 针对 <input type="datetime-local" value="2020-08-28T20:49:16">
     *
     * @param request HttpServletRequest
     * @param binder  binder
     */
    @InitBinder
    public void myDateBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }

    /**
     * 根据 cid 删除一个 合同
     *
     * @param cid 要删除的 cid
     * @return /view/contract/list
     */
    @RequestMapping("/deleteOneByCid.action")
    public String deleteOneByHid(Integer cid) {
        if (cid != null) {
            contractService.deleteOneByCid(cid);
        }
        return "view/contract/list";
    }


}
